// pages/BoosRank/BoosRank.js
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        ranktype: 1,//排行榜类型,1预约,2打赏
        reserva: {
            dateindex: 1,
            startDate: "",
            endDate: "",
            firstload:true,
            iscustom: false,
            listdata: [],
        },//预约数据
        reward: {
            dateindex: 1,
            startDate: "",
            endDate: "",
            firstload:true,
            iscustom: false,
            listdata: [],
        },//打赏数据
        reservaparam: {
            startDate: "",
            endDate: "",
        },
        rewardparam: {
            startDate: "",
            endDate: "",
        },
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var _this = this;
        var time = app.common.getTimeSection(1, "yyyy-MM-dd");
        _this.setData({
            "reservaparam": {
                "startDate": time.sdate,
                "endDate": time.edate
            },
            "rewardparam": {
                "startDate": time.sdate,
                "endDate": time.edate
            },
        });
        var ranktype = _this.data.ranktype;
        if (ranktype == 1) {
            _this.GetReservaList();
            _this.setData({
                "reserva.firstload": false
            })
        }
        if (ranktype == 2) {
            _this.GetRewardList();
            this.setData({
                "reward.firstload": false
            })
        }
    },
    //获取预约列表数据
    GetReservaList: function () {
        var _this = this;
        wx.showLoading({
            title: '加载中',
        });
        var param = _this.data.reservaparam;
        app.http.POST('/MiniManagerReserva/EmpRank', param, (err, data) => {
            if (data.code == 1) {
                //循环数据
                var list = data.data;
                for (var i = 0; i < list.length; i++) {
                    if (i == 0) { list[i]["topclass"] = "m-ranking-char a"; }
                    else if (i == 1) { list[i]["topclass"] = "m-ranking-char b"; }
                    else if (i == 2) { list[i]["topclass"] = "m-ranking-char c"; }
                    else { list[i]["topclass"] = "m-ranking-char d"; }
                }
                _this.setData({
                    "reserva.listdata": list
                });
                if (list == null || list.length < 1) {
                    _this.setData({
                        "reserva.nodata": true
                    });
                } else {
                    _this.setData({
                        "reserva.nodata": false
                    });
                }
            } else if (data.code == 2) {
                wx.showToast({
                    title: data.msg,
                    icon: 'none',
                    duration: 2000
                })
                _this.setData({
                    "reserva.listdata": [],
                    "reserva.nodata": false
                });
            }
            else {
                wx.showToast({
                    title: '请求失败',
                    icon: 'none',
                    duration: 2000
                })
                _this.setData({
                    "reserva.listdata": [],
                    "reserva.nodata": false
                });
            }
            wx.hideLoading();
        });
    },

    //获取打赏列表数据
    GetRewardList: function () {
        var _this = this;
        wx.showLoading({
            title: '加载中',
        });
        var param = _this.data.rewardparam;
        app.http.POST('/MiniManagerReward/EmpRank', param, (err, data) => {
            if (data.code == 1) {
                //循环数据
                var list = data.data;
                for (var i = 0; i < list.length; i++) {
                    if (i == 0) { list[i]["topclass"] = "m-ranking-char a"; }
                    else if (i == 1) { list[i]["topclass"] = "m-ranking-char b"; }
                    else if (i == 2) { list[i]["topclass"] = "m-ranking-char c"; }
                    else { list[i]["topclass"] = "m-ranking-char d"; }
                }
                _this.setData({
                    "reward.listdata": list
                });
                if (list == null || list.length < 1) {
                    _this.setData({
                        "reward.nodata": true
                    });
                } else {
                    _this.setData({
                        "reward.nodata": false
                    });
                }
            } else if (data.code == 2) {
                wx.showToast({
                    title: data.msg,
                    icon: 'none',
                    duration: 2000
                })
                _this.setData({
                    "reward.listdata": [],
                    "reward.nodata": false
                });
            }
            else {
                wx.showToast({
                    title: '请求失败',
                    icon: 'none',
                    duration: 2000
                })
                _this.setData({
                    "reward.listdata": [],
                    "reward.nodata": false
                });
            }
            wx.hideLoading();
        });
    },

    //排行类型切换
    RankChange: function (e) {
        var dataset = e.currentTarget.dataset;
        var id = parseInt(dataset.id);
        this.setData({
            ranktype: id
        });
        if(id==1)
        {
            var firstload = this.data.reserva.firstload;
            if (firstload) {
                this.setData({
                    "reserva.firstload": false
                })
                this.GetReservaList();
            }
        }
        if(id==2){
            var firstload = this.data.reward.firstload;
            if (firstload) {
                this.setData({
                    "reward.firstload":false
                })
                this.GetRewardList();
            }
        }
    },
    
    //日期类型筛选
    DateChange: function (e) {
        var _this = this;
        var dataset = e.currentTarget.dataset;
        var id = parseInt(dataset.id);
        var ranktype = _this.data.ranktype;
        if (ranktype == 1)//预约
        {
            var iscustom = _this.data.reserva.iscustom;
            if (id == 4) {//自定义选择时间
                iscustom = true;
                _this.setData({
                    "reserva.iscustom": iscustom,
                    "reserva.dateindex": id
                });
                if (_this.data.reserva.startDate != "") {
                    _this.setData({
                        "reservaparam.startDate": _this.data.reserva.startDate
                    });
                }
                if (_this.data.reserva.endDate != "") {
                    _this.setData({
                        "reservaparam.endDate": _this.data.reserva.endDate
                    });
                }
            }
            else {
                iscustom = false;
                var time = app.common.getTimeSection(id, "yyyy-MM-dd");
                if (id == 3) {
                    var mydate = new Date();
                    mydate = app.common.dateFormatType(mydate, 1);
                    time.sdate = "2016-01-01";
                    time.edate = mydate;
                }
                _this.setData({
                    "reserva.iscustom": iscustom,
                    "reserva.dateindex": id,
                    "reservaparam.startDate": time.sdate,
                    "reservaparam.endDate": time.edate
                })
                //调用预约列表
                this.GetReservaList();
            }
        }
        if (ranktype == 2)//打赏
        {
            var iscustom = _this.data.reserva.iscustom;
            if (id == 4) {//自定义选择时间
                iscustom = true;
                _this.setData({
                    "reward.iscustom": iscustom,
                    "reward.dateindex": id
                });
                if (_this.data.reward.startDate != "") {
                    _this.setData({
                        "rewardparam.startDate": _this.data.reward.startDate
                    });
                }
                if (_this.data.reward.endDate != "") {
                    _this.setData({
                        "rewardparam.endDate": _this.data.reward.endDate
                    });
                }
            }
            else {
                iscustom = false;
                var time = app.common.getTimeSection(id, "yyyy-MM-dd");
                if (id == 3) {
                    var mydate = new Date();
                    mydate = app.common.dateFormatType(mydate, 1);
                    time.sdate = "2016-01-01";
                    time.edate = mydate;
                }
                _this.setData({
                    "reward.iscustom": iscustom,
                    "reward.dateindex": id,
                    "rewardparam.startDate": time.sdate,
                    "rewardparam.endDate": time.edate
                })
                //调用打赏列表
                this.GetRewardList();
            }
        }
    },
    //开始时间筛选
    startDateChange: function (e) {
        var datestr = e.detail.value;
        var ranktype = this.data.ranktype;
        if (ranktype == 1) {
            this.setData({
                "reserva.startDate": datestr,
                "reservaparam.startDate": datestr
            });
        } if (ranktype == 2) {
            this.setData({
                "reward.startDate": datestr,
                "rewardparam.startDate": datestr
            });
        }

    },
    //结束时间筛选
    endDateChange: function (e) {
        var datestr = e.detail.value;
        var ranktype = this.data.ranktype;
        if (ranktype == 1) {
            this.setData({
                "reserva.endDate": datestr,
                "reservaparam.endDate": datestr
            });
        } if (ranktype == 2) {
            this.setData({
                "reward.endDate": datestr,
                "rewardparam.endDate": datestr
            });
        }
    },
    //自定义筛选
    CustomScreen: function () {
        var _this = this;
        var ranktype = _this.data.ranktype;
        if (ranktype == 1) {
            var startdate = _this.data.reserva.startDate;
            var enddate = _this.data.reserva.endDate;
            if (startdate == "" && enddate == "") {
                wx.showToast({
                    title: '请至少选择一个时间',
                    icon: 'none',
                    duration: 2000
                })
                return;
            }
            _this.GetReservaList();
        }
        if (ranktype == 2) {
            var startdate = _this.data.reward.startDate;
            var enddate = _this.data.reward.endDate;
            if (startdate == "" && enddate == "") {
                wx.showToast({
                    title: '请至少选择一个时间',
                    icon: 'none',
                    duration: 2000
                })
                return;
            }
            _this.GetRewardList();
        }

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        wx.setNavigationBarTitle({
            title: '排行榜'
        })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})